﻿using System;
using System.Web.Mvc;
using QBWeb.Models;
using System.Data.Entity;
using System.Linq;
using System.Net;
using QBWeb.ViewModels;
using System.Collections.Generic;
using Microsoft.Ajax.Utilities;
using QBWeb.BLL;
using Microsoft.AspNet.Identity;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
using QBWeb.Models.CurriculumFolder;
using QBWeb.Models.QuestionFolder;
using System.Data;

namespace QBWeb.Controllers
{
    /// <summary>
    /// 题目模块控制器类
    /// </summary>
    public class MainController : Controller
    {
        private readonly QBDbContext db = new QBDbContext();

        /// <summary>
        /// 题目详细信息的局部页面
        /// </summary>
        /// <param name="id"></param>
        /// <param name="questionType"></param>
        /// <returns></returns>
        public PartialViewResult Details(int id, int questionType)
        {
            var questionViewModel = new DetailsViewModels()
            {
                SingleChoiceTb = db.SingleChoiceTbs.Include(t => t.QuestionTb).Where(t => t.QuestionTbId == id).ToList(),
                TrueFalseTb = db.TrueFalseTbs.Include(t => t.QuestionTb).Where(t => t.QuestionTbId == id).ToList(),
                BlanksTb = db.BlanksTbs.Include(t => t.QuestionTb).Where(t => t.QuestionTbId == id).ToList()
            };
            ViewData.Model = questionViewModel;
            ViewBag.id_value = questionType;
            return PartialView("Details");
        }

        #region 老师管理题目功能
        /// <summary>
        /// 添加题目页面方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Add(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var testPaperTbs = db.TestPaperTbs.Find(id);

            var questionViewModels = new QuestionViewModels
            {
                TestPaperTb = testPaperTbs
            };
            return View(questionViewModels);
        }

        /// <summary>
        /// 添加题目方法
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <returns></returns>
        public JsonResult SaveResult(string jsonStr)
        {
            var jc = JsonConvert.DeserializeObject<AllQuestionModels>(jsonStr);
            var teacherId = User.Identity.GetUserId() ?? "temp";
            var cid = Convert.ToInt32(Session[teacherId]);
            InterfaceBll.SaveQuestions(jc, teacherId, cid);
            return Json(new { msg = "保存成功" });
        }

        /// <summary>
        /// 移除试卷中的题目的方法
        /// </summary>
        /// <param name="id">题目Id</param>
        /// <param name="questId">试卷Id</param>
        /// <returns></returns>
        public ActionResult RemoveQuestion(int id, int questId)
        {
            var test = db.TestDetails.Where(t => t.TestPaperTbId == id).ToList();
            //if (test.Count() == 0) throw new Exception("题目不存在");
            int testId = 0;
            foreach (var item in test)
            {
                if (item.QuestId == questId)
                {
                    testId = item.Id;
                }
            }
            object[] vs = { testId };
            string sql = "Delete from TestDetailsTbs where Id =@P0";
            try
            {
                db.Database.ExecuteSqlCommand(sql, vs);
            }
            catch
            {
                throw new Exception("删除失败");
            }
            return Json(new { msg = "删除成功" });
        }

        /// <summary>
        /// 教师获取题目数据
        /// </summary>
        /// <param name="id"></param>
        /// <param name="questId"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetQuestion(int id, string questId)
        {
            object str = SelectQuest(id, questId);

            return Json(str, JsonRequestBehavior.AllowGet);
        }
        #endregion

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="zsd"></param>
        ///// <param name="testDetailsId"></param>
        ///// <returns></returns>
        //public PartialViewResult Choose(string zsd, int testDetailsId)
        //{
        //    List<QuestionTb> cd = db.QuestionTbs.Where(m => m.SmallKnowModuleTbId.ToString() == zsd).Include(t=>t.QuestTypeTb).ToList();
        //    ViewData.Model = cd;
        //    string str = GetDate(testDetailsId);
        //    ViewBag.str = str;
        //    return PartialView("_ChooseQuestion");
        //}

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="testDetailsId"></param>
        ///// <returns></returns>
        //public string GetDate(int testDetailsId)
        //{
        //    TestDetailsTb testDetail = db.TestDetails.Find(testDetailsId);
        //    var date = db.TestDetails.Select(t => t.QuestId);
        //    return date.First();
        //}

        /// <summary>
        /// 获取所有的知识点
        /// </summary>
        /// <returns></returns>
        public ActionResult GetKnowledge()
        {
            var str = new List<object>();
            var url = Request.UrlReferrer.ToString();
            var cid = Convert.ToInt32(url.Substring(url.LastIndexOf("=") + 1));
            var subjects = db.SubjectTbs.Where(t => t.CurriculaId == cid).ToList();

            foreach (var x in subjects)
            {
                var bigKnow = db.BigKnowModuleTbs.Where(t => t.SubjectId == x.Id).Select(t => new SelectListItem
                {
                    Text = t.BigModuleName,
                    Value = t.Id.ToString()
                });
                foreach (var y in bigKnow)
                {
                    var smallKnow = db.SmallKnowModuleTbs.Where(t => t.BigKnowModuleTbId.ToString() == y.Value).Select(t => new SelectListItem
                    {
                        Text = t.SmallModuleName,
                        Value = t.Id.ToString()
                    });
                    str.Add(new
                    {
                        y.Text,
                        y.Value,
                        smallKnowledge = smallKnow
                    });
                }
            }
            return Json(str, JsonRequestBehavior.AllowGet);
        }
       

        #region 学生考试获取题目功能
        /// <summary>
        /// 学生获取题目数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetQuestionByStu(int id)
        {
            var str = FindQuest(id);

            return Json(str, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 考试获取题目，不返回答案
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public object FindQuest(int? id)
        {
            var sqlTools = new SetSqlTools();
            var str = new object();
            if (id!=null)
            {
                var testDetails = db.TestDetails.Where(t => t.TestPaperTbId == id).ToList();
                if (testDetails.Count == 0) return Json(str, JsonRequestBehavior.AllowGet);
                var questItem = "";
                for (int i = 0; i < testDetails.Count; i++)
                {
                    questItem += testDetails[i].QuestId;
                }
                if (questItem == "")
                {
                    return Json(str, JsonRequestBehavior.AllowGet);
                }
                str = sqlTools.GetDataByStu(id);
            }
           
            return str;
        }
        #endregion

        /// <summary>
        /// 搜索题目
        /// </summary>
        /// <param name="id"></param>
        /// <param name="a"></param>
        /// <returns></returns>
        public object SelectQuest(int id,string a)
        {
            var sqlTools = new SetSqlTools();
            var str = new object();
            if (a.IsNullOrWhiteSpace())//判断是单题，还是试卷题，a为空则为试卷题。
            {
                var testDetails = db.TestDetails.Where(t => t.TestPaperTbId == id).ToList();
                if (testDetails.Count== 0) return Json(str, JsonRequestBehavior.AllowGet);
                var questItem = "";
                for (int i = 0; i < testDetails.Count; i++)
                {
                    questItem += testDetails[i].QuestId;
                }
                if (questItem == "")
                {
                    return Json(str, JsonRequestBehavior.AllowGet);
                }
                str = sqlTools.GetDbQuestionByTestId(id);
            }
            else
            {
                str = sqlTools.GetDbQuestionByQuestId(a);
            }
            return str;
        }

        #region 手动组卷功能
        /// <summary>
        /// 选项卡返回页面
        /// </summary>
        /// <param name="CardIndex"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ResultCard(int CardIndex)
        {
            switch (CardIndex)
            {
                case 0:
                    var questionViewModel = new QuestionViewModels
                    {
                        QuestionTb = new QuestionTb(),
                        QuestionTbs = db.QuestionTbs.Include(t => t.QuestTypeTb).ToList(),
                    };
                    ViewData.Model = questionViewModel;
                    return PartialView("_Card1");
                case 1:
                    return PartialView("_Card2");
                default:
                    return PartialView("_Card1");
            }
        }

        /// <summary>
        /// 返回所有题目
        /// </summary>
        /// <returns></returns>
        public ActionResult ResultAllQuestion()
        {
            var str = new List<object>();
            var url = Request.UrlReferrer.ToString();
            var index = url.LastIndexOf("=");
            var cid = Convert.ToInt32(url.Substring(index + 1));
            var subjects = db.SubjectTbs.Where(t => t.CurriculaId == cid).ToList();
            var questionTb = new List<QuestionTb>();
            foreach (var x in subjects)
            {
                var bigKnow = db.BigKnowModuleTbs.Where(t => t.SubjectId == x.Id).ToList();
                foreach (var y in bigKnow)
                {
                    var smallKnow = db.SmallKnowModuleTbs.Where(t => t.BigKnowModuleTbId == y.Id).ToList();
                    foreach (var z in smallKnow)
                    {
                        questionTb = db.QuestionTbs.Include(t => t.QuestTypeTb).Where(t => t.SmallKnowModuleTbId == z.Id).ToList();
                        foreach (var i in questionTb)
                        {
                            str.Add(new
                            {
                                QuestionId = i.Id,
                                i.QuestName,
                                SmallKnowledge = i.SmallKnowModuleTbId,
                                QuestType = i.QuestTypeTbId,
                                i.QuestTypeTb.TypeName,
                                Level = i.QuestLevel,
                                CreateTime = i.CreateTime.ToString()
                            });
                        }
                    }
                }
            }
            return Json(str, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 返回所有的知识点
        /// </summary>
        /// <returns></returns>
        public ActionResult ResualAllKnow()
        {
            List<object> str = new List<object>();
            List<BigKnowModuleTb> bigKnow = db.BigKnowModuleTbs.ToList();
            foreach (var x in bigKnow)
            {
                var smallKnow = db.SmallKnowModuleTbs.Where(t => t.BigKnowModuleTbId == x.Id).ToList();
                foreach (var y in smallKnow)
                {
                    str.Add(new
                    {
                        KnowID = y.Id,
                        BigKnowID = y.BigKnowModuleTbId,
                        BigKnowName = y.BigKnowModuleTb.BigModuleName,
                        SmallKnowName = y.SmallModuleName
                    });
                }
            }
            return Json(str, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 运用于随机组卷
        /// </summary>
        /// <param name="testDetailsId"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Yunyong(int id, string knowId, int questnumber, int judgenumber, int choosenumber, int fillInBlanksnumber, string difficultys)//运用
        {
            var b = ChoseKnow(knowId, questnumber, judgenumber, choosenumber, fillInBlanksnumber, difficultys);
            var len = b.LastIndexOf(';');
            var a = b.Substring(0, len);
            var times = Convert.ToInt32(b.Substring(len + 1, b.Length - len - 1));
            var str = SelectQuest(id, a);
            return Json(new { question = str, time = times });
        }

        /// <summary>
        /// 获取随机到的题目，返回一个字符串
        /// </summary>
        /// <param name="knowId"></param>
        /// <param name="questnumber"></param>
        /// <param name="judgenumber"></param>
        /// <param name="choosenumber"></param>
        /// <param name="fillInBlanksnumber"></param>
        /// <param name="difficultys"></param>
        /// <returns></returns>
        public string ChoseKnow(string knowId, int questnumber, int judgenumber, int choosenumber, int fillInBlanksnumber, string difficultys)
        {
            var quesType = new List<int>();
            if (choosenumber != 0) quesType.Add(1);//若传过来的值不等于0，则将题目类型加入
            if (judgenumber != 0) quesType.Add(2);
            if (fillInBlanksnumber != 0) quesType.Add(3);
            var quesTypes = quesType.ToArray();
            var knowIDs = knowId.Split(';');
            var sq = new Random();
            //题型随机
            var qt = new int[questnumber];//放置随机好的题型
            for (var i = 0; i < questnumber; i++)//存在死循环情况
            {
                int choose = 0, judge = 0, fillInBlanks = 0;
                var e = quesTypes[sq.Next(0, quesTypes.Length)];
                if (e == 1)
                {
                    if (choose <= choosenumber)
                    {
                        qt[i] = e;
                        choose++;

                    }
                    else
                    {
                        i--;
                        continue;
                    }
                }
                else if (e == 2)
                {
                    if (judge <= judgenumber)
                    {
                        qt[i] = e;
                        judge++;
                    }
                    else
                    {
                        i--;
                        continue;
                    }
                }
                else if (e == 3)
                {
                    if (fillInBlanks <= fillInBlanksnumber)
                    {
                        qt[i] = e;
                        fillInBlanks++;
                    }
                    else
                    {
                        i--;
                        continue;
                    }
                }
            }
            //知识点随机
            var know = new int[questnumber];//放置随机好的知识点
            for (var j = 0; j < questnumber; j++)
            {
                var x = sq.Next(0, knowIDs.Length);
                var e = Convert.ToInt32(knowIDs[x]);
                know[j] = e;
            }
            //难度随机
            int one = 0, two = 0, three = 0, four = 0, five = 0;
            var oneNumber = questnumber / 10;//平均分成10等分，每份多少题
            var remaining = questnumber % 10;//剩下的部分用来分配
            if (difficultys == "low")//简单难度
            {
                if (remaining < 4)
                {
                    one = oneNumber * 4 + remaining;
                    two = oneNumber * 4;
                    three = oneNumber * 2;
                    four = 0;
                    five = 0;
                }
                else if (remaining < 7)
                {
                    one = oneNumber * 4 + 3;
                    two = oneNumber * 4 + remaining - 3;
                    three = oneNumber * 2;
                    four = 0;
                    five = 0;
                }
                else
                {
                    one = oneNumber * 4 + 4;
                    if (remaining < 9)
                    {
                        two = oneNumber * 4 + remaining - 4;
                        three = oneNumber * 2;
                        four = 0;
                        five = 0;
                    }
                    else
                    {
                        two = oneNumber * 4 + 4;
                        three = oneNumber * 2 + remaining - 8;
                        four = 0;
                        five = 0;
                    }
                }
            }
            else if (difficultys == "middle")//中难度
            {
                if (remaining < 3)
                {
                    one = oneNumber * 2 + remaining;
                    two = oneNumber * 2;
                    three = oneNumber * 2;
                    four = oneNumber * 2;
                    five = oneNumber * 2;
                }
                else if (remaining < 5)
                {
                    one = oneNumber * 2 + 2;
                    two = oneNumber * 2 + remaining - 2;
                    three = oneNumber * 2;
                    four = oneNumber * 2;
                    five = oneNumber * 2;
                }
                else if (remaining < 7)
                {
                    one = oneNumber * 2 + 2;
                    two = oneNumber * 2 + 2;
                    three = oneNumber * 2 + remaining - 4;
                    four = oneNumber * 2;
                    five = oneNumber * 2;
                }
                else if (remaining < 9)
                {
                    one = oneNumber * 2 + 2;
                    two = oneNumber * 2 + 2;
                    three = oneNumber * 2 + 2;
                    four = oneNumber * 2 + remaining - 6;
                    five = oneNumber * 2;
                }
                else
                {
                    one = oneNumber * 2 + 2;
                    two = oneNumber * 2 + 2;
                    three = oneNumber * 2 + 2;
                    four = oneNumber * 2 + 2;
                    five = oneNumber * 2 + remaining - 8;
                }
            }
            else if (difficultys == "high")//高难度
            {
                if (remaining < 2)
                {
                    one = oneNumber * 1 + remaining;
                    two = oneNumber * 1;
                    three = oneNumber * 2;
                    four = oneNumber * 3;
                    five = oneNumber * 3;
                }
                else if (remaining < 3)
                {
                    one = oneNumber * 1 + 1;
                    two = oneNumber * 1 + remaining - 1;
                    three = oneNumber * 2;
                    four = oneNumber * 3;
                    five = oneNumber * 3;
                }
                else if (remaining < 5)
                {
                    one = oneNumber * 1 + 1;
                    two = oneNumber * 1 + 1;
                    three = oneNumber * 2 + remaining - 2;
                    four = oneNumber * 3;
                    five = oneNumber * 3;
                }
                else if (remaining < 7)
                {
                    one = oneNumber * 1 + 1;
                    two = oneNumber * 1 + 1;
                    three = oneNumber * 2 + 2;
                    four = oneNumber * 3 + remaining - 4;
                    five = oneNumber * 3;
                }
                else
                {
                    one = oneNumber * 1 + 1;
                    two = oneNumber * 1 + 1;
                    three = oneNumber * 2 + 2;
                    four = oneNumber * 3 + 2;
                    five = oneNumber * 3 + remaining - 6;
                }
            }
            int[] questLevel = { 1, 2, 3, 4, 5 };//难度
            int oneNumbers = 0, twoNumbers = 0, threeNumbers = 0, fourNumbers = 0, fiveNumbers = 0;
            var ql = new int[questnumber];//难度
            for (var item = 0; item < questnumber; item++)//随机难度
            {
                int x = sq.Next(0, questLevel.Length);
                int e = questLevel[x];
                if (e == 1)
                {
                    if (oneNumbers < one)
                    {
                        ql[item] = e;
                        oneNumbers++;
                    }
                    else
                    {
                        item--;
                        continue;
                    }
                }
                else if (e == 2)
                {
                    if (twoNumbers < two)
                    {
                        ql[item] = e;
                        twoNumbers++;
                    }
                    else
                    {
                        item--;
                        continue;
                    }
                }
                else if (e == 3)
                {
                    if (threeNumbers < three)
                    {
                        ql[item] = e;
                        threeNumbers++;
                    }
                    else
                    {
                        item--;
                        continue;
                    }
                }
                else if (e == 4)
                {
                    if (fourNumbers < four)
                    {
                        ql[item] = e;
                        fourNumbers++;
                    }
                    else
                    {
                        item--;
                        continue;
                    }
                }
                else if (e == 5)
                {
                    if (fiveNumbers < five)
                    {
                        ql[item] = e;
                        fiveNumbers++;
                    }
                    else
                    {
                        item--;
                        continue;
                    }
                }
            }

            var n = new int[questnumber, 2];//放置最终的题目
            //int[,] combination = new int[questnumber, 3];//将表中每列数据当成一个数组存入二维数组
            var combinationStr = new string[questnumber];//将二维数组中的内容转换成字符串

            var numberOfCalculations1 = new int[questnumber];//将记录的字符串放入数组，用于后面取最大值
            var questType = db.Database.SqlQuery<int>("select QuestTypeTbId from QuestionTbs group by QuestTypeTbId order by count(QuestTypeTbId) desc").ToList();
            var questLevel2 = db.Database.SqlQuery<int>("select  QuestLevel from QuestionTbs group by QuestLevel order by count(QuestLevel) desc").ToList();
            var smallKnow2 = db.Database.SqlQuery<int>("select  SmallKnowModuleTbId from QuestionTbs group by SmallKnowModuleTbId order by count(SmallKnowModuleTbId) desc").ToList();
            for (var x = 0; x < questnumber; x++)//题目条件组合
            {
                //combination[x, 0] = qt[x];
                //combination[x, 1] = know[x];
                //combination[x, 2] = ql[x];
                combinationStr[x] = Convert.ToString(qt[x] + "," + know[x] + "," + ql[x]);
            }

            for (var item = 0; item < questnumber; item++)
            {
                var numberOfCalculations = 0;//记录字符串重复的次数
                for (var item1 = 0; item1 < questnumber; item1++)
                {
                    if (combinationStr[item] == combinationStr[item1])
                    {
                        numberOfCalculations++;
                        numberOfCalculations1[item] = numberOfCalculations;
                    }
                }
            }
            var max = 0;
            foreach (var item in numberOfCalculations1)
                max = max > item ? max : item;

            int original = 0, num = 0, through = 0, replace = 0;
            for (var b = 0; b < questnumber; b++)
            {
                if (n[b, 1] > 20) through = 0;
                object[] param = { qt[b], know[b], ql[b] };
                const string sql1 = "select Id from QuestionTbs where QuestTypeTbId=@P0 and SmallKnowModuleTbId=@P1 and QuestLevel=@P2";
                var a = db.Database.SqlQuery<int>(sql1, param).ToList();
                through++;
                if (a.Count() != 0) //符合条件的数据行数,存在错误,返回题目的长度=0时
                {
                    var c = a[sq.Next(0, a.Count())];
                    //通过随机获取题目
                    if (Rechecking(n, c) == false)//判断题目是否存在,自己定义的查重方法
                    {
                        n[b, 0] = c;
                        n[b, 1] = through;
                    }
                    else
                    {
                        original++;
                        if (original > max)
                        {
                            num++;
                            if (num <= 3)
                            {
                                n[b, 1] = through;
                                n = SelectId(questType[0], know[b], ql[b], n, b);
                            }
                            else if (num <= 6)
                            {
                                n[b, 1] = through;
                                n = SelectId(qt[b], smallKnow2[0], ql[b], n, b);
                            }
                            else if (num <= 9)
                            {
                                n[b, 1] = through;
                                n = SelectId(qt[b], know[0], questLevel2[0], n, b);
                            }
                            else
                            {
                                if (through > 9)
                                {
                                    n = SelectEveryId(n, b);
                                }
                                num = 0;
                            }
                            if (n[b, 0] == 0)
                                b--;
                            else
                                replace++;
                        }
                        else
                            b--;
                    }
                }
                else
                {
                    num++;
                    if (num <= 3)
                    {
                        n[b, 1] = through;
                        n = SelectId(questType[0], know[b], ql[b], n, b);
                    }
                    else if (num <= 6)
                    {
                        n[b, 1] = through;
                        n = SelectId(qt[b], smallKnow2[0], ql[b], n, b);
                    }
                    else if (num <= 9)
                    {
                        n[b, 1] = through;
                        n = SelectId(qt[b], know[0], questLevel2[0], n, b);
                    }
                    else
                    {
                        if (through > 9)
                        {
                            n = SelectEveryId(n, b);
                        }
                        num = 0;
                    }

                    if (n[b, 0] == 0)
                    {
                        b--;
                    }
                    else
                    {
                        replace++;
                    }
                }
            }
            var w = new int[questnumber];
            var end = new int[questnumber];
            for (var i = 0; i < questnumber; i++)
            {
                w[i] = n[i, 0];
            }
            var sdr = string.Join(",", w);
            var questionTbs = db.Database.SqlQuery<QuestionTb>("select * from QuestionTbs where id in(" + sdr + ") order by QuestTypeTbId,QuestLevel").ToList();
            for (var j = 0; j < questnumber; j++)
            {
                end[j] = questionTbs[j].Id;
            }
            var str = string.Join(";", end);
            str = str + ";" + replace;//replace是因为满足条件的题目数量不够，替换其他题目的题数
            return str;

        }

        /// <summary>
        /// 当无法满足随机到的条件时，将一个条件换成该条件最多的时候，例如题型中填空题最多，就将题型的条件替换成填空题
        /// </summary>
        /// <param name="questType"></param>
        /// <param name="know"></param>
        /// <param name="questLevel"></param>
        /// <param name="n"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        private int[,] SelectId(int questType, int know, int questLevel, int[,] n, int b)
        {
            var id = db.Database.SqlQuery<int>("select Id from QuestionTbs where QuestTypeTbId=" + questType + " and SmallKnowModuleTbId=" + know + " and QuestLevel=" + questLevel).ToList();
            var sq = new Random();
            if (id.Count == 0) return n;
            var c2 = Convert.ToInt32(id[sq.Next(0, id.Count())]);
            if (!Rechecking(n, c2))
            {
                n[b, 0] = c2;
            }
            return n;
        }

        /// <summary>
        /// 查重方法
        /// </summary>
        /// <param name="n"></param>
        /// <param name="c"></param>
        /// <returns></returns>
        private static bool Rechecking(int[,] n, int c)
        {
            var b = false;
            for (var i = 0; i < n.Length / 2; i++)
            {
                if (n[i, 0] == c)
                {
                    b = true;//查到数组中有找的值
                }
            }
            return b;
        }

        /// <summary>
        /// 没有任何条件满足时，直接从题库中随机抽取一题
        /// </summary>
        /// <param name="n"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        private int[,] SelectEveryId(int[,] n, int b)
        {
            var id = db.Database.SqlQuery<int>("select Id from QuestionTbs").ToList();
            var sq = new Random();
            if (id.Count() != 0)
            {
                var c2 = Convert.ToInt32(id[sq.Next(0, id.Count())]);
                if (Rechecking(n, c2) == false)
                {
                    n[b, 0] = c2;
                }
            }
            return n;
        }
        #endregion

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}